using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_App_Client.MainManagers
{
	#region using
	using DIS_App_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_App_Client.DIS_WS_BusinessLayer;
	#endregion
	public partial class FrmManageWeeklyReport : Form
	{
		public FrmManageWeeklyReport(bool enableChon = false)
		{
			InitializeComponent();
		}

		private void FrmManageWeeklyReport_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.INSERT, ref errorText))
			{
				MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionAdd())
			{
                MessageBox.Show(CommonHelper.GetMessage("Insert", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
				LoadData();
				btnReset_Click(sender, e);
			}
			else
			{
                MessageBox.Show(CommonHelper.GetMessage("Insert", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnReset_Click(object sender, EventArgs e)
		{
			txtID.Text = "";
			//dtStartDate.Value = DateTime.MinValue;
			//dtEndDate.Value = DateTime.MinValue;
		}

		private void dtgvData_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (dtgvData.SelectedRows == null || dtgvData.SelectedRows.Count == 0)
				return;
			var selectedItem = dtgvData.SelectedRows[0];
			txtID.Text = dtgvData.SelectedRows[0].Cells["ID"].Value.ToString();
			var item = BusService.Instance.WeeklyReport_SelectByID(int.Parse(txtID.Text));
			LoadDetailInfo(item);
		}

		public void LoadDetailInfo(WeeklyReportDTO item)
		{
			dtStartDate.Value = item.StartDate;
			dtEndDate.Value = item.EndDate;
            txtNote.Text = item.Note;
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ChemistWareHouse",
				DataPropertyName = "ChemistWareHouse",
				Name = "ChemistWareHouse",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "StartDate",
				DataPropertyName = "StartDate",
				Name = "StartDate",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "EndDate",
				DataPropertyName = "EndDate",
				Name = "EndDate",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}

		private void LoadData()
		{
			var lstItem = BusService.Instance.WeeklyReport_SelectByIDChemistWareHouse(GlobalVariables.g_CurrentUser.IDChemistWareHouse);
			var dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("ChemistWareHouse");
			dt.Columns.Add("StartDate");
			dt.Columns.Add("EndDate");
			for (int i = 0; i < lstItem.Length; i++)
			{
				dt.Rows.Add(lstItem[i].ID, lstItem[i].ChemistWareHouse.Name, String.Format("{0:dd/MM/yyyy}", lstItem[i].StartDate), String.Format("{0:dd/MM/yyyy}", lstItem[i].EndDate));
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private bool ValidateInput(EnumUpdateMode mode, ref string errorText)
		{
            if (txtID.Text != string.Empty)
            {
                errorText = "Cannot update exist report";
                return false;
            }
            if (dtStartDate.Value == DateTime.MinValue)
            {
                errorText = "Please select start date";
                return false;
            }
            if (dtStartDate.Value.DayOfWeek != DayOfWeek.Monday)
            {
                errorText = "Start date must be monday";
                return false;
            }
            if (dtEndDate.Value == DateTime.MinValue)
            {
                errorText = "Please select end date";
                return false;
            }
            if (dtEndDate.Value.DayOfWeek != DayOfWeek.Sunday)
            {
                errorText = "End date must be sunday";
                return false;
            }
            if (dtStartDate.Value > dtEndDate.Value)
            {
                errorText = "Start date larger than end date";
                return false;
            }

            var lstDate = CommonHelper.GetDateRange(dtStartDate.Value, dtEndDate.Value);

            if (lstDate.Count > 7)
            {
                errorText = "Start date and end date is not same in a week";
                return false;
            }

            var report = BusService.Instance.WeeklyReport_SelectByIDChemistWareHouseAndReportDate(GlobalVariables.g_CurrentUser.IDChemistWareHouse, dtStartDate.Value, dtEndDate.Value);
            if (report != null)
            {
                errorText = "There is exist daily report for range of date";
                return false;
            }

            var numOrders = 0;
            foreach (var date in lstDate)
            {
                var lstItem = BusService.Instance.Orders_SelectByIDChemistWareHouseAndOrderDate(GlobalVariables.g_CurrentUser.IDChemistWareHouse, date);
                numOrders += lstItem.Length;
                if (numOrders > 0)
                {
                    return true;
                }
            }

            if (numOrders == 0)
            {
                errorText = "Range of date does not have any orders";
                return false;
            }

			return true;
		}

		private bool ActionAdd()
		{
			try
			{
				var item = new WeeklyReportDTO
				{
					IDChemistWareHouse = GlobalVariables.g_CurrentUser.IDChemistWareHouse,
					StartDate = dtStartDate.Value,
					EndDate = dtEndDate.Value,
                    Note = txtNote.Text
				};
				if (!BusService.Instance.WeeklyReport_Insert(item))
				{
					return false;
				}
				return true;
			}
			catch
			{
				return false;
			}
		}
	}
}